﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using TestOpenGL.OpenGL;
using System.Reflection;
using TestOpenGL.OpenGLES;

namespace WiMo.Games.OpenGLDisplay.OpenGL
{
    public class Test
    {
        Texture _image;
        Texture _image2;

        const int count = 8;
        const float height = 0.25f;
        //const float size = 2.5f / count;
        const float size = .25f;


        static float[] triangle = new float[] { -size, -height, size, -height, -size, height, size, height };
        static float[] texCoords = new float[] { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f };


        public void Init()
        {
            _image = Texture.LoadStream(
    Assembly.GetExecutingAssembly().GetManifestResourceStream("WiMo.Games.OpenGLDisplay.TestResources.Ball1.png"), true);

            _image2 = Texture.LoadStream(
    Assembly.GetExecutingAssembly().GetManifestResourceStream("WiMo.Games.OpenGLDisplay.TestResources.Ball2.png"), true);
        }


        unsafe public void Draw(float rotation)
        {
            gl.Enable(gl.GL_ALPHA_TEST);
            gl.Enable(gl.GL_TEXTURE_2D);
            gl.BindTexture(gl.GL_TEXTURE_2D, _image2.Name);
            gl.PushMatrix();
            gl.Rotatef(10, 1.0f, 0.0f, 0.0f);
            gl.Rotatef(rotation, 0.0f, 1.0f, 0.0f);

            float angleInc = 360.0f / count;
            fixed (float* trianglePointer = &triangle[0], texCoordsPointer = &texCoords[0])
            {
                gl.EnableClientState(gl.GL_VERTEX_ARRAY);
                gl.VertexPointer(2, gl.GL_FLOAT, 0, (IntPtr)trianglePointer);
                gl.EnableClientState(gl.GL_TEXTURE_COORD_ARRAY);
                gl.TexCoordPointer(2, gl.GL_FLOAT, 0, (IntPtr)texCoordsPointer);
            }

            bool odd = true;

            for (int i = 0; i < count; ++i)
            {
                if (odd)
                    gl.BindTexture(gl.GL_TEXTURE_2D, _image.Name);
                else
                    gl.BindTexture(gl.GL_TEXTURE_2D, _image2.Name);

                odd = !odd;

                float angle = i * angleInc;
                gl.PushMatrix();
                gl.Rotatef(angle, 0.0f, 1.0f, 0.0f);
                gl.Translatef(0.0f, 0.0f, 0.7f);
                gl.DrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4);
                gl.PopMatrix();
            }
            gl.DisableClientState(gl.GL_VERTEX_ARRAY);
            gl.DisableClientState(gl.GL_TEXTURE_COORD_ARRAY);

            gl.PopMatrix();
            gl.Disable(gl.GL_TEXTURE_2D);
            gl.Disable(gl.GL_ALPHA_TEST);
        }
    }
}
